package k8s

import (
	"context"

	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	core "k8s.io/client-go/kubernetes/typed/apps/v1"
)

func NewDeployment(cli *kubernetes.Clientset) *Deployment {
	return &Deployment{
		core: cli.AppsV1(),
	}
}

type Deployment struct {
	core core.AppsV1Interface
}

// 获取Deployment列表
func (s *Deployment) GetDeployments(ctx context.Context, namespace string, req *v1.ListOptions) (*appsv1.DeploymentList, error) {
	return s.core.Deployments(namespace).List(ctx, *req)
}

// 获取Deployment详情
func (s *Deployment) GetDeploymentDetails(ctx context.Context, namespace, name string) (*appsv1.Deployment, error) {
	return s.core.Deployments(namespace).Get(ctx, name, v1.GetOptions{})
}

// 创建Deployment
func (s *Deployment) CreateDeployment(ctx context.Context, namespace string, deployment *appsv1.Deployment) (*appsv1.Deployment, error) {
	return s.core.Deployments(namespace).Create(ctx, deployment, v1.CreateOptions{})
}

// 更新Deployment
func (s *Deployment) UpdateDeployment(ctx context.Context, namespace string, deployment *appsv1.Deployment) (*appsv1.Deployment, error) {
	return s.core.Deployments(namespace).Update(ctx, deployment, v1.UpdateOptions{})
}

// 删除Deployment
func (s *Deployment) DeleteDeployment(ctx context.Context, namespace, name string) error {
	return s.core.Deployments(namespace).Delete(ctx, name, v1.DeleteOptions{})
}
